package com.jy.adscm.task.job;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.jy.adscm.exception.AdscmSystemException;
import com.jy.adscm.order.bean.JyOrder;
import com.jy.adscm.order.bean.JyProduct;
import com.jy.adscm.order.mapper.JyOrderMapper;
import com.jy.adscm.order.mapper.JyProductMapper;
import com.jy.adscm.stock.bean.JyInStorage;
import com.jy.adscm.stock.mapper.JyInStorageMapper;
import com.jy.adscm.utils.AdscmDateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

/**
 * @author zyl
 * @Date 2019/1/31 -9:53
 */
@Component
public class JyOrderSignJob {

    private Logger logger = Logger.getLogger(getClass());
    @Autowired
    JyOrderMapper jyOrderMapper;

    @Autowired
    JyProductMapper  jyProductMapper;
    @Autowired
    JyInStorageMapper jyInStorageMapper;
     @Transactional
    @Scheduled(cron = "0 0/1 * * * ? ")
    protected void signOrder() throws AdscmSystemException {
         //查询出当天需要完成签收的订单
        logger.info("开始签收今日需要签收的订单");
        Wrapper<JyOrder> jyOrderWrapper=new EntityWrapper<>();
        String nowOnlyDateFomat1 = AdscmDateUtils.getNowOnlyDateFomat1(new Date());
        jyOrderWrapper.like("last_sign_time",nowOnlyDateFomat1, SqlLike.RIGHT);
        jyOrderWrapper.isNull("sign_time");
        jyOrderWrapper.eq("order_state",3);
        List<JyOrder> jyOrders = jyOrderMapper.selectList(jyOrderWrapper);
        for (JyOrder jyOrder : jyOrders) {
            jyOrder.setSignPerson(jyOrder.getCreator());
            jyOrder.setSignTime(new Date());
            jyOrderMapper.updateById(jyOrder);
            logger.info(jyOrder.getOrderNum()+"已自动签收");
            //生成入库信息
            JyInStorage jyInStorage =new JyInStorage();
            jyInStorage.setOrderNum(jyOrder.getOrderNum());
            jyInStorage.setUnit(jyOrder.getUnit());
            jyInStorage.setInstorageNum(jyOrder.getNum());
            jyInStorage.setInstorageTime(new Date());
            jyInStorage.setOrderTime(jyOrder.getCreateTime());
            jyInStorage.setProduct(jyOrder.getProduct());
            JyProduct jyProduct = jyProductMapper.selectById(jyOrder.getProduct());
            jyInStorage.setType(jyProduct.getType());
            jyInStorage.setTypeNum(jyProduct.getTypeNum());
            jyInStorage.setDealer(jyOrder.getDealer());
            jyInStorageMapper.insert(jyInStorage);
            logger.info(jyInStorage.getOrderNum()+"生成入库信息");
        }

    }
}
